package com.example.mydemo928;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Wang on 13-9-28.
 * 标准的View定义方法
 */
public class LIViewWithSpaceNameAndStandrd extends LinearLayout {
    String labText;
    Bitmap bitmap;
    public LIViewWithSpaceNameAndStandrd(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray;
        /**
         * 将布局中的值都封装在 TypedArray 
         *  获取布局中xml设定的值：
         *  1、通过typedArray.getResourceId 获取id
         *  2、通过这个 typedArray.getXXX(id) 获取所需要的值
         */
        typedArray = context.obtainStyledAttributes(attrs,R.styleable.LIViewWithSpaceNameAndStandrd);

       
        
        int resourseId;
        resourseId=typedArray.getResourceId(R.styleable.LIViewWithSpaceNameAndStandrd_labText, -1);
       if (resourseId!=-1) {
		labText=typedArray.getString(resourseId);
	}
      
        if (labText == null){
            throw  new RuntimeException("必须设置labText 参数");
        }

        resourseId=typedArray.getResourceId(R.styleable.LIViewWithSpaceNameAndStandrd_ico, 0);
        System.out.println("  "+R.styleable.LIViewWithSpaceNameAndStandrd_ico+" "+resourseId);
        Bitmap bitmap = null;
        if (0 != resourseId) {
             bitmap=BitmapFactory.decodeResource(getResources(), resourseId);

		}
        
        LayoutInflater inflater;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linearLayout= (LinearLayout) inflater.inflate(R.layout.layoutitem,this);
        TextView textView= (TextView) findViewById(R.id.textview1);
        ImageView imageView= (ImageView) findViewById(R.id.img);
        textView.setText(labText);
        if (null != bitmap){
 
            imageView.setImageBitmap(bitmap);
        }
    }

}
